class SolarSystem {
    constructor() {
        this.canvas = document.getElementById('solarSystem');
        this.ctx = this.canvas.getContext('2d');
        this.planetInfo = document.getElementById('planetInfo');
        
        // 设置画布大小
        this.resize();
        window.addEventListener('resize', () => this.resize());
        
        // 行星数据
        this.planets = [
            { name: "水星", radius: 60, size: 6, color: "#b5b5b5", speed: 0.047, angle: 0 },
            { name: "金星", radius: 90, size: 10, color: "#e6c200", speed: 0.035, angle: 0 },
            { name: "地球", radius: 120, size: 12, color: "#2a5caa", speed: 0.03, angle: 0 },
            { name: "火星", radius: 150, size: 8, color: "#d94f2a", speed: 0.024, angle: 0 },
            { name: "木星", radius: 200, size: 22, color: "#e3a857", speed: 0.013, angle: 0 },
            { name: "土星", radius: 260, size: 18, color: "#e6d690", speed: 0.01, angle: 0 },
            { name: "天王星", radius: 320, size: 14, color: "#6bc2e4", speed: 0.007, angle: 0 },
            { name: "海王星", radius: 380, size: 14, color: "#3b6ba5", speed: 0.005, angle: 0 },
            { name: "冥王星", radius: 440, size: 4, color: "#cccccc", speed: 0.003, angle: 0 }
        ];

        // 绑定事件
        this.bindEvents();
        
        // 开始动画
        this.animate();
    }

    resize() {
        this.canvas.width = Math.min(window.innerWidth - 40, 1000);
        this.canvas.height = Math.min(window.innerHeight - 40, 1000);
        this.centerX = this.canvas.width / 2;
        this.centerY = this.canvas.height / 2;
    }

    bindEvents() {
        this.canvas.addEventListener('mousemove', (e) => {
            const rect = this.canvas.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;
            
            // 检查鼠标是否在行星上
            for (const planet of this.planets) {
                const planetX = this.centerX + planet.radius * Math.cos(planet.angle);
                const planetY = this.centerY + planet.radius * Math.sin(planet.angle);
                const distance = Math.hypot(x - planetX, y - planetY);
                
                if (distance < planet.size * 2) {
                    this.planetInfo.style.display = 'block';
                    this.planetInfo.style.left = (e.clientX + 20) + 'px';
                    this.planetInfo.style.top = (e.clientY + 20) + 'px';
                    this.planetInfo.textContent = planet.name;
                    return;
                }
            }
            this.planetInfo.style.display = 'none';
        });
    }

    drawSun() {
        // 绘制太阳
        const gradient = this.ctx.createRadialGradient(
            this.centerX, this.centerY, 0,
            this.centerX, this.centerY, 30
        );
        gradient.addColorStop(0, '#fff');
        gradient.addColorStop(0.3, '#ffff00');
        gradient.addColorStop(1, '#ff4500');
        
        this.ctx.beginPath();
        this.ctx.arc(this.centerX, this.centerY, 30, 0, Math.PI * 2);
        this.ctx.fillStyle = gradient;
        this.ctx.fill();
        
        // 添加光晕效果
        this.ctx.shadowColor = '#ff4500';
        this.ctx.shadowBlur = 40;
        this.ctx.fill();
        this.ctx.shadowBlur = 0;
    }

    drawOrbit(radius) {
        this.ctx.beginPath();
        this.ctx.arc(this.centerX, this.centerY, radius, 0, Math.PI * 2);
        this.ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
        this.ctx.stroke();
    }

    drawPlanet(planet) {
        const x = this.centerX + planet.radius * Math.cos(planet.angle);
        const y = this.centerY + planet.radius * Math.sin(planet.angle);
        
        // 绘制行星
        this.ctx.beginPath();
        this.ctx.arc(x, y, planet.size, 0, Math.PI * 2);
        
        // 创建行星渐变
        const gradient = this.ctx.createRadialGradient(
            x - planet.size/3, y - planet.size/3, planet.size/10,
            x, y, planet.size
        );
        gradient.addColorStop(0, '#fff');
        gradient.addColorStop(1, planet.color);
        
        this.ctx.fillStyle = gradient;
        this.ctx.fill();
        
        // 添加行星光晕
        this.ctx.shadowColor = planet.color;
        this.ctx.shadowBlur = 10;
        this.ctx.fill();
        this.ctx.shadowBlur = 0;
    }

    animate() {
        // 清空画布
        this.ctx.fillStyle = '#000';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
        
        // 绘制太阳
        this.drawSun();
        
        // 更新和绘制行星
        for (const planet of this.planets) {
            // 绘制轨道
            this.drawOrbit(planet.radius);
            
            // 更新行星角度
            planet.angle += planet.speed;
            
            // 绘制行星
            this.drawPlanet(planet);
        }
        
        // 继续动画循环
        requestAnimationFrame(() => this.animate());
    }
}

// 初始化太阳系
window.onload = () => {
    new SolarSystem();
}; 